package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.UsersMapper;
import com.pojo.TbUsers;
import com.pojo.TbWorker;
import com.service.UsersService;
import com.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, TbUsers> implements UsersService {
    @Autowired
    JSONResult jsonResult;

    @Override
    public JSONResult findAllUserByUserName(int page, int limit, String userName) {
        Page<TbUsers> tbpage = new Page<>(page,limit);
        if (userName!=null&&userName!=""){
            QueryWrapper<TbUsers> tbUsers = new QueryWrapper<>();
            tbUsers.eq("user_name",userName);
            Page<TbUsers> page1 = this.page(tbpage,tbUsers);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        Page<TbUsers> page1 = this.page(tbpage);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }

    @Override
    public JSONResult findAllUserByState(int page, int limit, int state) {
        Page<TbUsers> tbpage = new Page<>(page,limit);
        if (state==1){
            QueryWrapper<TbUsers> tbUsers = new QueryWrapper<>();
            tbUsers.eq("user_state",state);
            Page<TbUsers> page1 = this.page(tbpage,tbUsers);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }else if (state==0){
            Page<TbUsers> page1 = this.page(tbpage);
            return jsonResult.success(200,"查询成功",page1,page1.getTotal());
        }
        return jsonResult.failure(201,"状态码错误,查询失败",null,null);
    }

    @Override
    public JSONResult addUser(TbUsers user) {
        boolean save = this.save(user);
        if (save){
            return jsonResult.success(200,"添加成功",null,1L);
        }
        return jsonResult.failure(201,"添加失败",null,0L);
    }

    @Override
    public JSONResult deleteUser(int userId) {
        boolean b = this.removeById(userId);
        if (b){
            return jsonResult.success(200);
        }
        return jsonResult.failure(201);
    }

    @Override
    public JSONResult updateUser(TbUsers user) {
        boolean b = this.saveOrUpdate(user);
        if (b){
            return jsonResult.success(200,"修改成功",null,null);
        }
        return jsonResult.failure(201,"修改失败",null,null);
    }

    @Override
    public JSONResult findUserById(int userId) {
        QueryWrapper<TbUsers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        TbUsers user = this.getOne(queryWrapper);
        if (user!=null){
            return jsonResult.success(200,"查询成功",user,null);
        }
        return jsonResult.failure(201,"查询失败",null,null);
    }

    @Override
    public JSONResult findAllUser(int page, int limit) {
        Page<TbUsers> tbpage = new Page<>(page,limit);
        QueryWrapper<TbUsers> tbProducts = new QueryWrapper<>();
        Page<TbUsers> page1 = this.page(tbpage,tbProducts);
        return jsonResult.success(200,"查询成功",page1,page1.getTotal());
    }
}
